﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using ClassLibraryEntidades;
using ClassLibraryServer.Negocio;
using System.Data;

namespace ClassLibraryServer.Datos
{
    public class DLTopologia
    {
        private SqlConnection connection;

        
        #region Constructor Singlenton
        private static DLTopologia instance = null;
        public static DLTopologia Instance
        {
            get
            {
                if (instance == null)
                    instance = new DLTopologia();
                return instance;
            }
        }

        private DLTopologia()
        {

            string cadenaConexion = ConfigurationManager.ConnectionStrings["conexionSQLExpress"].ConnectionString;
            this.connection = new SqlConnection(cadenaConexion); 
        }


        #endregion


        public Nodo DameNodo(Direccion direccion)
        {
            Nodo nodo=null;
            SqlDataReader dataReader = null;

            string busquedaNodoPorDireccion = "SELECT * FROM NODOS WHERE NUMERO=@numero AND CALLE=@calle";
            SqlCommand command_buscarNodoPorDireccion = new SqlCommand(busquedaNodoPorDireccion, this.connection);

            try
            {
                this.connection.Open();
                command_buscarNodoPorDireccion.Parameters.Add("@calle", SqlDbType.VarChar, direccion.calle.Length).Value = direccion.calle;
                command_buscarNodoPorDireccion.Parameters.Add("@numero", SqlDbType.Int).Value=direccion.numero;

                dataReader = command_buscarNodoPorDireccion.ExecuteReader();
                if (dataReader.HasRows)
                {
                    dataReader.Read();                    
                    Coordenada coordenada = new Coordenada(Convert.ToInt32(dataReader["X"]), Convert.ToInt32(dataReader["Y"]) );
                    nodo = new Nodo(coordenada, direccion);

                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
            finally
            {
                dataReader.Close();
                this.connection.Close();
            }
            

            return nodo;
        }
        public Nodo DameNodo(Coordenada coordenada)
        {
            Nodo nodo=null;

            this.connection.Open();



            this.connection.Close();

            return nodo;
        }

        public Ruta DameRuta(int id_nodo_origen, int id_nodo_destino)
        {
            string dameRuta = "SELECT * FROM RUTAS WHERE ORIGEN=@origen AND DESTINO=@destino";
            SqlCommand command_buscarNodoPorDireccion = new SqlCommand(dameRuta, this.connection);

            return null;
        }
    }
}
